package com.gisocn.onemap.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import com.esri.core.geometry.ShapeModifiers;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.base.MyApplication;
import com.gisocn.onemap.sgzx.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/* loaded from: classes2.dex */
public class AppVersionManager implements PreferenceManager.OnActivityResultListener {
    private static final int APK_SIZE = 7;
    private static final int CANCEL_UPDATE_VERSION = 6;
    private static final int CHECK_VERSION_FINISH = 5;
    private static final int DISMISS_DOWNLOAD_DIALOG = 2;
    private static final int DOWNLOADING = 3;
    private static final int DOWNLOAD_FINISH = 4;
    private static final int DOWN_OVER = 2;
    private static final int DOWN_UPDATE = 1;
    private static final int REQ_INSALL_APK = 8;
    private static final int SHOW_DOWNLOAD_DIALOG = 1;
    private static final String TAG = "AppVersionManager";
    private static final String savaFileName;
    private static final String savaPath;
    private Activity context;
    private Thread downLoadThread;
    private Dialog downloadDialog;
    private IUpdataResult iUpdataResult;
    private IVersionCallback mCallBacks;
    private ProgressBar mProgress;
    private AppVersionManager manager;
    private TextView tvInfo;
    private TextView tvSize;
    private TextView updateText;
    private boolean autoUpdate = false;
    private StringBuffer sbInfo = new StringBuffer();
    private int progress = 0;
    private boolean interceptFlag = false;
    private boolean isDownOver = false;
    private String downLoadUrl = "";
    private String versionDesc = "";
    private Handler handler = new Handler() { // from class: com.gisocn.onemap.utils.AppVersionManager.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            switch (message.what) {
                case 1:
                    AppVersionManager.this.showDownLoadDialog();
                    return;
                case 2:
                    AppVersionManager.this.dismissDownloadDialog();
                    return;
                case 3:
                    AppVersionManager.this.mProgress.setProgress(message.arg1);
                    return;
                case 4:
                    AppVersionManager.this.installApk((File) message.obj);
                    return;
                case 5:
                    if (AppVersionManager.this.mCallBacks != null) {
                        AppVersionManager.this.mCallBacks.checkVersionFinish();
                        return;
                    }
                    return;
                case 6:
                    if (AppVersionManager.this.mCallBacks != null) {
                        AppVersionManager.this.mCallBacks.checkVersionCanceled();
                        return;
                    }
                    return;
                case 7:
                    AppVersionManager.this.tvInfo.setText(AppVersionManager.this.sbInfo.toString());
                    AppVersionManager.this.tvSize.setText((String) message.obj);
                    return;
                default:
                    return;
            }
        }
    };
    private Handler mHandler = new Handler() { // from class: com.gisocn.onemap.utils.AppVersionManager.4
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i != 1) {
                if (i != 2) {
                    return;
                }
                AppVersionManager.this.mProgress.setProgress(100);
                AppVersionManager.this.updateText.setText("下载完成");
                AppVersionManager.this.dismissDownloadDialog();
                AppVersionManager.this.installApk();
                return;
            }
            AppVersionManager.this.mProgress.setProgress(AppVersionManager.this.progress);
            TextView textView = AppVersionManager.this.updateText;
            textView.setText("正在下载更新包" + AppVersionManager.this.progress + "%");
        }
    };
    private Runnable mdownApkRunnable = new Runnable() { // from class: com.gisocn.onemap.utils.AppVersionManager.5
        @Override // java.lang.Runnable
        public void run() {
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(AppVersionManager.this.getDownLoadUrl()).openConnection();
                httpURLConnection.connect();
                int contentLength = httpURLConnection.getContentLength();
                InputStream inputStream = httpURLConnection.getInputStream();
                File file = new File(AppVersionManager.savaPath);
                if (!file.exists()) {
                    file.mkdir();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(new File(AppVersionManager.savaFileName));
                byte[] bArr = new byte[1024];
                int i = 0;
                while (true) {
                    int read = inputStream.read(bArr);
                    i += read;
                    AppVersionManager.this.progress = (int) ((i / contentLength) * 100.0f);
                    AppVersionManager.this.mHandler.sendEmptyMessage(1);
                    if (read <= 0) {
                        AppVersionManager.this.isDownOver = true;
                        AppVersionManager.this.mHandler.sendEmptyMessage(2);
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                    if (AppVersionManager.this.interceptFlag) {
                        break;
                    }
                }
                fileOutputStream.close();
                inputStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
                AppVersionManager.this.handler.post(new Runnable() { // from class: com.gisocn.onemap.utils.AppVersionManager.5.1
                    @Override // java.lang.Runnable
                    public void run() {
                        AppVersionManager.this.dismissDownloadDialog();
                        Toast.makeText(AppVersionManager.this.context, "对不起，更新失败！", 1).show();
                        AppVersionManager.this.handler.sendEmptyMessage(5);
                    }
                });
            } catch (IOException e2) {
                e2.printStackTrace();
                AppVersionManager.this.handler.post(new Runnable() { // from class: com.gisocn.onemap.utils.AppVersionManager.5.2
                    @Override // java.lang.Runnable
                    public void run() {
                        AppVersionManager.this.dismissDownloadDialog();
                        Toast.makeText(AppVersionManager.this.context, "对不起，更新失败！", 1).show();
                        AppVersionManager.this.handler.sendEmptyMessage(5);
                    }
                });
            }
        }
    };

    /* loaded from: classes2.dex */
    public interface IUpdataResult {
        void updataResult();
    }

    /* loaded from: classes2.dex */
    public interface IVersionCallback {
        void checkVersionCanceled();

        void checkVersionFinish();
    }

    static {
        String str = Constant.SAVE_PATH;
        savaPath = str;
        savaFileName = str + "一张图.apk";
    }

    public AppVersionManager(Activity activity) {
        this.context = activity;
    }

    public void registerCallback(IVersionCallback iVersionCallback) {
        this.mCallBacks = iVersionCallback;
    }

    public void updaResult(IUpdataResult iUpdataResult) {
        this.iUpdataResult = iUpdataResult;
    }

    public void setUpdateType(boolean z) {
        this.autoUpdate = z;
    }

    public int getAppVersion() {
        try {
            return this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public String getAppVersionName() {
        try {
            return this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void dismissDownloadDialog() {
        Dialog dialog = this.downloadDialog;
        if (dialog == null || !dialog.isShowing()) {
            return;
        }
        this.downloadDialog.dismiss();
    }

    private void downLoadApk() {
        Thread thread = new Thread(this.mdownApkRunnable);
        this.downLoadThread = thread;
        thread.start();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void installApk(File file) {
        if (file.exists()) {
            System.out.println(file.toString());
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
            intent.setFlags(ShapeModifiers.ShapeHasIDs);
            this.context.startActivityForResult(intent, 8);
            dismissDownloadDialog();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void installApk() {
        File file = new File(savaFileName);
        if (file.exists()) {
            Uri fromFile = Uri.fromFile(file);
            Intent intent = new Intent();
            intent.addFlags(ShapeModifiers.ShapeHasIDs);
            intent.setAction("android.intent.action.VIEW");
            if (Build.VERSION.SDK_INT >= 24) {
                Uri uriForFile = FileProvider.getUriForFile(MyApplication.mContext, Constant.FILEPROVIDER_AUTHORITY, file);
                intent.addFlags(1);
                intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(fromFile, "application/vnd.android.package-archive");
            }
            this.context.startActivity(intent);
        }
    }

    public void upDateAppVersion(String str, String str2) {
        this.versionDesc = str2;
        this.downLoadUrl = str;
        this.handler.post(new Runnable() { // from class: com.gisocn.onemap.utils.AppVersionManager.2
            @Override // java.lang.Runnable
            public void run() {
                RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(AppVersionManager.this.context).inflate(R.layout.dialog_clean_message, (ViewGroup) null);
                AlertDialog.Builder builder = new AlertDialog.Builder(AppVersionManager.this.context);
                builder.setView(relativeLayout);
                final AlertDialog create = builder.create();
                create.setCancelable(false);
                if (AppVersionManager.this.context != null && AppVersionManager.this.context.getWindow().getDecorView().getVisibility() == 0) {
                    create.show();
                }
                ((TextView) relativeLayout.findViewById(R.id.tv_content)).setText("检测到" + AppVersionManager.this.versionDesc + "新版本，是否更新?");
                ((Button) relativeLayout.findViewById(R.id.btn_clean_message_cancel)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.utils.AppVersionManager.2.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        create.dismiss();
                        AppVersionManager.this.handler.sendEmptyMessage(6);
                        if (AppVersionManager.this.iUpdataResult != null) {
                            AppVersionManager.this.iUpdataResult.updataResult();
                        }
                    }
                });
                ((Button) relativeLayout.findViewById(R.id.btn_clean_message_ok)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.utils.AppVersionManager.2.2
                    @Override // android.view.View.OnClickListener
                    public void onClick(View view) {
                        create.dismiss();
                        AppVersionManager.this.handler.sendEmptyMessage(1);
                    }
                });
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String getDownLoadUrl() {
        return this.downLoadUrl;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showDownLoadDialog() {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this.context).inflate(R.layout.progress, (ViewGroup) null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
        builder.setView(linearLayout).setCancelable(false);
        AlertDialog create = builder.create();
        this.downloadDialog = create;
        create.show();
        this.mProgress = (ProgressBar) linearLayout.findViewById(R.id.progress);
        this.updateText = (TextView) linearLayout.findViewById(R.id.updateText);
        ((Button) linearLayout.findViewById(R.id.btn_cancel)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.utils.AppVersionManager.3
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                AppVersionManager.this.downloadDialog.dismiss();
                AppVersionManager.this.interceptFlag = true;
                AppVersionManager.this.iUpdataResult.updataResult();
            }
        });
        downLoadApk();
    }

    @Override // android.preference.PreferenceManager.OnActivityResultListener
    public boolean onActivityResult(int i, int i2, Intent intent) {
        if (i != 8) {
            return true;
        }
        this.handler.sendEmptyMessage(5);
        return true;
    }
}
